import RandTool from "../../common/tool/RandTool";
import ContextPool from "../../common/tool/context/ContextPool";
import GameSnake from "../GameSnake";
import BodyCube from "../body/BodyCube";
import BodyFood from "../body/BodyFood";
import LocalCommon from "../config/LocalCommon";
import MapBase from "./MapBase";
const { ccclass, property } = cc._decorator;
@ccclass
export default class CubeMap extends MapBase {

    @property
    public CubeNumber: number = 0;
    @property
    public FoodNumber: number = 0;

    @property(cc.Prefab)
    public PrefabCube: cc.Prefab = null;
    @property(cc.Prefab)
    public PrefabFood: cc.Prefab = null;


    public init(content: ContextPool): boolean {
        super.init(content);
        this.createGroup();
        return true;
    }

    public createGroup() {
        let positonList = this.getPostionXArray(this.CubeNumber + this.FoodNumber);

        let cubeNumberList = this.getCubeNumberList();
        let left = LocalCommon.CubeWidth * 0.5
        for (let i = 0; i < this.CubeNumber; i++) {
            let node = cc.instantiate(this.PrefabCube);
            let cube = node.getComponent(BodyCube);
            node.position = cc.v3(positonList[i] + left, 0);
            cube.init(this.mContextPool);
            cube.Number = cubeNumberList[i];

            this.node.addChild(node);
        }

        for (let j = 0; j < this.FoodNumber; j++) {
            var food = cc.instantiate(this.PrefabFood);
            food.position = cc.v3(positonList[j + this.CubeNumber] + LocalCommon.CubeWidth * 0.5, 0);
            let foodControl = food.getComponent(BodyFood);
            foodControl.init(this.mContextPool);
            this.node.addChild(food);
        }
    }


    public getPostionXArray(number: number): Array<number> {
        number = Math.min(number, LocalCommon.MAX_ROW_CUBE);
        let result: Array<number> = [];
        for (let i = 0; i < number; i++) {
            result.push(i * LocalCommon.CubeWidth);
        };
        this.shuffleSelf(result);
        return result;
    }
    shuffleSelf(array: Array<any>): void {
        for (let i = array.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [array[i], array[j]] = [array[j], array[i]];
        }
    }

    public getCubeNumberList(): Array<number> {
        let life = this.getContext(GameSnake).SnakeLength;
        let cubeModelNum = new Array();
        let min = 2;
        if (life > 10)
            min = 6;
        cubeModelNum.push(1 + RandTool.RandIncludeAll(0, min));
        cubeModelNum.push(2 + RandTool.RandIncludeAll(0, min));
        cubeModelNum.push(5 + RandTool.RandIncludeAll(0, min));
        cubeModelNum.push(10 + RandTool.RandIncludeAll(0, min));
        cubeModelNum.push(15 + RandTool.RandIncludeAll(0, min));
        this.shuffleSelf(cubeModelNum);

        let reuslt = [];
        for (var i = 0; i < this.CubeNumber; i++) {
            reuslt.push(cubeModelNum[i]);
        }
        return reuslt;
    }
}
